<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">智能制造平台</h3>
      <el-button @click="tab1" :class="li == tab ? 'bgc2' : 'bgc1'" type="info" style="border-radius: 0%;">域账号登录
      </el-button>
      <el-button @click="tab2" type="info" style="border-radius: 0%;" ref="job" :class="li == tab ? 'bgc1' : 'bgc2'">
        工号登录</el-button>
      <el-form-item prop="username" label-width="80px" :label="li === tab ? '工号' : '域账号'">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="工号">
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码" label-width="80px" v-if="tab">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
          @keyup.enter.native="handleLogin">
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button :loading="loading" size="medium" style="width: 20%;margin-left: 100px; "
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登录</span>
          <span v-else>登录中</span>
        </el-button>
        <el-button type="success" plain style="width: 30%;margin-left: 25px;" size="medium"
          @click.native.prevent="handleLoginfanke">工号访客</el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2022 ICT.riming All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { login, getCodeImg, getverifyEmpNo } from "@/api/login";
import Cookies from "js-cookie";
// 加密与解密的方法
import { encrypt, decrypt } from '@/utils/jsencrypt';

export default {
  name: "Login",
  data() {
    return {
      // 用于实现切换登录,作为判断
      isShowIndex: 0,
      tab: true,
      li: false,
      codeUrl: "",
      // 个人信息
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      // 验证信息
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" }
        ],
        code: [
          { required: true, trigger: "change", message: "请输入验证码" }
        ],
        uuid: [
          {required: true, trigger: "blur", message: "请输入工号"}
        ],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  // 登录后重定向到原先路由(带参数)
  watch: {
    $route: {
      handler: function (route) {
        // console.log("跳转0", route);
        // console.log("跳转1", route.query);
        // console.log("跳转2", route.query.redirect);
        this.redirect = route.query && route.query.redirect;
        // console.log("跳转4", this.redirect);
      },
      immediate: true
    }
  },
  created() {
    // this.getCode();
    this.getCookie();
  },
  methods: {
    // 域账号登录
    Logintab() {
      this.tab = true;
      this.isShowIndex = 0;
    },
    // 工号登录
    Logintar() {
      this.tab = false;
      this.isShowIndex = 1;
    },
    // 验证码
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          // 把返回的图片拼接base64编码的gif图片数据
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    // 校验Cookie身份信息
    getCookie() {
      // 获取Cookie个人身份信息账号
      const username = Cookies.get("username");
      // 获取Cookie个人身份信息密码
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      // 渲染到输入框
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        // 对password进行加密
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      }
    },
    // 获取登录方法
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          console.log("获取登录信息", this.loginForm);
          if (this.loginForm.password) {
            // Cookies有效期30天
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            console.log("测试重定向", this.redirect);
            this.$router.push({path: this.redirect || "/"}).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if(this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    // 工号登录方法
    handleLoginfanke() {
      this.$router.push({ path: this.redirect || "/" }).catch(() => {});
      // console.log("测试工号",this.loginForm.username)
      // let sysUser = {
      //     "employeeId": "",
      // }
      // sysUser.employeeId=this.loginForm.username;
      // let strId = JSON.stringify(sysUser);
      // console.log("测试tempIdData",sysUser)
      // console.log("测试strId",strId)
      // getverifyEmpNo(strId).then(res => {
      //   console.log("测试接口成功",res)
      //   sessionStorage.setItem("usernamefangkeSSO", JSON.stringify(res.data));
      //   console.log("测试重定向",this.redirect);
      //   this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      // });
    },
    // 关闭方法
    handleColse() {
      this.collectdialogVisible = false;
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  color: black;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #070707;
}

.el-button {
  width: 50%;
  margin-bottom: 20px;
}

.login-form {
  border-radius: 6px;
  background: #dbdada;
  width: 400px;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #050404;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0px;
  width: 100%;
  text-align: center;
  color: rgb(8, 8, 8);
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.bgc1 {
  background-color: #050404;
  color: white;
  border: 0px;
  margin-left: 0px !important;
}

.bgc2 {
  background-color: #ffffff;
  color: rgb(10, 10, 10);
  border: 0;
  margin-left: 0px !important;

}
</style>
